package com.payment.ktb.activity.main4;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.MainActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsSettle;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/6/12.
 */
public class NocardSettleResultWaitActivity extends BaseActivity {

    int count = 0;


    @BindView(R.id.tv_nocardsettleresultwait_amount)
    TextView tv_nocardsettleresultwait_amount;
    @BindView(R.id.tv_nocardsettleresultwait_time)
    TextView tv_nocardsettleresultwait_time;
    @BindView(R.id.tv_nocardsettleresultwait_settleno)
    TextView tv_nocardsettleresultwait_settleno;
    @BindView(R.id.tv_nocardsettleresultwait_preTime)
    TextView tv_nocardsettleresultwait_preTime;



    @Override
    protected void onPause() {
        super.onPause();
        count = 0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_nocardsettleresultwait);
        ButterKnife.bind(this);
        hideTitleBack();
        initData();
        querySettleState();
        initTitle("结算状态");
    }


    /**
     *
     */
    private void initData() {
        tv_nocardsettleresultwait_amount.setText(ConstantsSettle.amount);
        tv_nocardsettleresultwait_time.setText(ConstantsSettle.settleTime);
        tv_nocardsettleresultwait_settleno.setText(ConstantsSettle.settleNo);
        tv_nocardsettleresultwait_preTime.setText("预到账时间：" + ConstantsSettle.preTime);
    }


    @OnClick({R.id.btn_nocardsettleresultwait_back,R.id.btn_nocardsettleresultwait_refresh})
    public void clickEvent(View view) {

        switch (view.getId()) {



            //返回商户
            case R.id.btn_nocardsettleresultwait_back:
                startActivity(MainActivity.class);
                break;

            case R.id.btn_nocardsettleresultwait_refresh:
                querySettleState();
                break;

        }



    }








    /**
     * 查询订单状态
     */
    private void querySettleState() {
        count ++;
        progressDialogUtils.showProgress("正在查询订单状态...");


        //构造传输参数
        String settleNo = ConstantsSettle.settleNo;
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        String sign = Md5Utils.md5Hex(settleNo + SharedPreferencesUtils.getString(ConstantsUser.userKey));



        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();

        map.put("settleNo",settleNo);
        map.put("token",token);
        map.put("sign",sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.nocardSettleQueryState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {



                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {
                        String settleStatus = jsonObject.optString("settleStatus");
                        ConstantsSettle.settleTime = jsonObject.optString("settleTime");
                        tv_nocardsettleresultwait_time.setText(ConstantsSettle.settleTime);
                        ConstantsSettle.preTime = jsonObject.optString("preTime");
                        tv_nocardsettleresultwait_preTime.setText("预到账时间：" +ConstantsSettle.preTime);
                        if ("20".equals(settleStatus)) {
                            progressDialogUtils.dismissProgress();
                            startActivity(NocardSettleResultSuccessActivity.class);
                        } else if ("21".equals(settleStatus)) {
                            progressDialogUtils.dismissProgress();
                            startActivity(NocardSettleResultFailActivity.class);
                            ConstantsSettle.settleReason = jsonObject.optString("settleReason");
                        } else {
                            if (count < 4) {
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        querySettleState();
                                    }
                                }, 4000);
                            } else {
                                progressDialogUtils.dismissProgress();
                                initData();
                            }
                        }

                    } else {
                        progressDialogUtils.dismissProgress();
                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    progressDialogUtils.dismissProgress();
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });


    }



}
